package com.css.fxfzxczh.modules.disasterDamageInfo.repository.impl;

import com.css.fxfzxczh.modules.disasterDamageInfo.entity.DisasterDamage;
import com.css.fxfzxczh.modules.disasterDamageInfo.repository.DisasterDamageRepository;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @Author:
 * @CreateTime: 2024/4/22 14:20
 * @Description:
 */
@Repository
public class DisasterDamageRepositoryImpl implements DisasterDamageRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void batchSave(List<DisasterDamage> list) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());

        String sql = "insert into xczh_disaster_damage_data (id,occurrence_time,longitude,latitude,province," +
                "city,county,magnitude,loss_value,house_loss,renovation_loss," +
                "room_loss,lifeline_loss,enterprises_loss,disaster_loss,create_user,create_time,county_code,county_geom) " +
                "values (:id,:occurrenceTime,:longitude,:latitude,:province,:city,:county,:magnitude," +
                ":lossValue,:houseLoss,:renovationLoss,:roomLoss,:lifelineLoss,:enterprisesLoss,:disasterLoss," +
                ":createUser,:createTime,:countyCode,st_geomfromtext(:countyGeom,4490))";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public Map<String, Object> getCountyInfo(BigDecimal longitude, BigDecimal latitude) {
        String sql = "SELECT ST_AsText(geom) as geom,code FROM district_boundary_county " +
                "WHERE ST_Within(st_geomfromtext('POINT("+longitude+" "+latitude+")',4490),geom) limit 1;";
        Map<String, Object> map = jdbcTemplate.queryForMap(sql);
        return map;
    }
}
